(function(){
	var obj = {};
	var barCharts = function(id, url, option, json, fn){
		this.options = $.extend({
			color: '#000',
			title:{
				x:'center',
			    textStyle:{
			    	color: '#fff',
			    	fontSize: '16'
			    },
			    padding: 0
			},
			tooltip:{
				trigger:'axis',
			},
			legend: {
			    x: "left",
			    y: "top",
			    z: 0,
			    textStyle:{
		            color: '#000'
		        }
			},
			toolbox:{
				show:true,
				y:'center',
				orient : 'vertical',
				z:0,
				feature: {
                    //图表类型切换
                    magicType: {//动态类型切换
                        type: ['bar', 'line']//bar:柱状图，line:线状图
                    },
                 	 //保存图片
                    saveAsImage: {
                        show: true
                    },
                    //刷新
                    restore: {
                        show: true
                    }
                }
			},
			xAxis:{
				type: 'category',
				axisLabel: {
				   interval:0,  
				   rotate:15,
				   textStyle:{
					   color: '#000',
					   fontSize:'12'
				   }
				} 
			},
			yAxis:{
				type : 'value',
				splitLine: {show:false},
				axisLabel: {
				   textStyle:{
					   color: '#000'
				   }
				} 
			}
		}, option);
		this.init(id, url, json, fn);
	};
	barCharts.prototype = {
		init: function(id, url, json, fn){
			if(id){
				if(!url){
					url = mini.get("url").getValue();
				}
				//获取后台数据
				var flag = this.getValueByService(this, url, fn);
				if(flag){
					if($("#no_data")){
						$("#no_data").remove();
					}
					var myChart = echarts.init(document.getElementById(id));
					myChart.setOption(this.option(this, json), true);
				}else{
					$("#"+id).html("<div id=\"no_data\" style=\"margin:10% auto; text-align: center; color: #BFBFBF; font-size: 1.8em;\">暂无数据!</div>");
					myChart = "";
				}
			}
		},
		getValueByService: function(el, url, fn){
			var flag = false;
			if(!url){
				url = "getPublicEchartsData.json?1=1";
			}
			if(mini.get("solutionid")){
				url += "&solutionid="+mini.get("solutionid").getValue();
			}
			if(mini.get("type")){
				url += "&type="+mini.get("type").getValue().replace(/\s+/g,"");
			}
	    	if(mini.get("topsearchkey")){
	    		var topsearchkeyArr = mini.get("topsearchkey").getValue().split(',');
	    		if(topsearchkeyArr){
	        		for(var i=0,l=topsearchkeyArr.length; i<l ; i++){
	            		var key = topsearchkeyArr[i];
	            		if(key && mini.get(key)){
	            			var value = mini.get(key).getValue();
	    					if(value){
	    						url += "&"+key+"="+encodeURI( value );
	    					}
	            		}
	            	}
	        	}
	    	}
			$.ajax({
				url: url,
				type: "post",
				async: false,
				dataType: "json",
				success: function (result) {
					var json = result.root;
					if(result.xarray){
						el.options.title.text = json.title == null ? "" : json.title;
						//el.options.subTitleText = json.subtitletext == null ? "" : json.subtitletext;
						
						var legends = json.legendarray == null ? "" : json.legendarray;
						el.options.legend.data = legends == "" ? [] : legends.split(",");
				
						el.options.xAxis.data = result.xarray == null ? [] : result.xarray;
						el.options.yAxis.name = '单位:' + (json.yname == null ? "" : json.yname);
						
						var colors = json.colorarray == null ? [] : json.colorarray;
						el.options.color = colors == "" ? ["#2464C3"] : colors.split(",");
						obj.dataArray = result.dataarray == null ? [] : result.dataarray;
						obj.nameArray = result.namearray == null ? [] : result.namearray;
						flag = true;
					}
					if(typeof fn === "function"){
						fn(result);
					}
				},
				error: function (){
					alert("操作失败!");
				}
			});
			return flag;
		},
		option: function(el, json){
			var name = obj.nameArray;
			var series = [];
			if(name && name.length>0){
				for(var i=0; i<name.length; i++){
					var data = {
						z:0,
						barMaxWidth:30,
						type:'bar',
						markPoint : {
			                data : [
			                    {type : 'max', name: '最大值'},
			                    {type : 'min', name: '最小值'}
			                ]
			            },
			            markLine : {
			                data : [
			                    {type : 'average', name: '平均值'}
			                ]
			            }
					};
					data["name"] = name[i];
					if(json){
						data = $.extend(data, json);
					}
					var arr = [];
					arr.push.apply(arr, obj.dataArray[i]);
					data["data"] = arr;
					series.push(data);
				}
				el.options.series = series;
			}
			return el.options;
		}
	}
	window.barCharts=barCharts;
})();

